<?php
/*********************************************************************
    class.type.php

    Task type helper

    vim: expandtab sw=4 ts=4 sts=4:
    $Id: $
**********************************************************************/

/*
 * Mainly used as a helper...
 */

class Type {
    var $id;
    var $type_name;
    var $dept_id;
	var $type_desc;
    var $rate;
	var $active;
	

 
    var $info;
    
    function Type($id,$fetch=true){
        $this->id=$id;
        if($fetch)
            $this->load();
    }

    function load() {

        if(!$this->id)
            return false;
        
        $sql='SELECT * FROM '.TYPE_TABLE.' WHERE type_id='.db_input($this->id);
        if(($res=db_query($sql)) && db_num_rows($res)) {
            $info=db_fetch_array($res);
            $this->id=$info['type_id'];
            $this->type=$info['type_name'];
			$info['type']=$info['type_name'];
            $this->dept_id=$info['dept_id'];
            $this->type_desc=$info['type_desc'];
			$this->rate=$info['rate'];
            $this->active=$info['isactive'];
            $this->info=$info;
            return true;
        }
        $this->id=0;
        
        return false;
    }
  
    function reload() {
        return $this->load();
    }
    
    function getId(){
        return $this->id;
    }
    
    function getName(){
        return $this->type;
    }
    
    function getDeptId() {
        return $this->dept_id;
    }

    function getTypeDescription() {
        return $this->type_desc;
    }
    
    function getRate() {
        return $this->rate;
    }

    function isEnabled() {
         return $this->active?true:false;
    }

    function isActive(){
        return $this->isEnabled();
    }

    function getInfo() {
        return $this->info;
    }

    function update($vars,&$errors) {
        if($this->save($this->getId(),$vars,$errors)){
            $this->reload();
            return true;
        }
        return false;
    }

    function create($vars,&$errors) { 
        return Type::save(0,$vars,$errors);
    }

    function save($id,$vars,&$errors) {
        if($id && $id!=$vars['type_id'])
            $errors['err']='Internal error. Try again';

        if(!$vars['type'])
            $errors['type']='Task type required';
        elseif(strlen($vars['type'])<5)
            $errors['type']='Task type is too short. 5 chars minimum';
        else{
		
			$sql='SELECT type_id FROM '.TYPE_TABLE.' WHERE type_name='.db_input(Format::striptags($vars['type']));
            if($id)
                $sql.=' AND type_id!='.db_input($id);
            if(($res=db_query($sql)) && db_num_rows($res))
                $errors['type']='Task type already exists';
        }
            
        if(!$vars['dept_id'])
            $errors['dept_id']='You must select a department';
            
        if(!$vars['rate']){
            $errors['rate']='You must specify task rate.';
		}else{
			$vars['rate'] = str_replace(',','.',$vars['rate']);
			if (!is_numeric($vars['rate']))
				$errors['rate']='Incorect number format.';
		}
            
        if(!$errors) {
            $sql='updated=NOW(),type_name='.db_input(Format::striptags($vars['type'])).
                 ',dept_id='.db_input($vars['dept_id']).
                 ',task_desc='.db_input($vars['type_desc']).
                 ',isactive='.db_input($vars['isactive']).
				 ',rate='.db_input($vars['rate']);
            if($id) {
                $sql='UPDATE '.TYPE_TABLE.' SET '.$sql.' WHERE type_id='.db_input($id);
                if(!db_query($sql) || !db_affected_rows())
                    $errors['err']='Unable to update task type. Internal error occured';
            }else{
                $sql='INSERT INTO '.TYPE_TABLE.' SET '.$sql.',created=NOW()';
                if(!db_query($sql) or !($typeID=db_insert_id()))
                    $errors['err']='Unable to create the task type. Internal error';
                else
                    return $typeID;
            }
        }

        return $errors?false:true;
    }
}
?>
